package com.budwk.app.sqfw.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.budwk.app.sqfw.models.SqfwNotice;
import com.budwk.app.sqfw.models.Sqfw_rally;
import com.budwk.app.sqfw.services.SqfwCenSusPaperService;
import com.budwk.app.sqfw.services.SqfwNoticeService;
import com.budwk.app.sqfw.services.SqfwRallyService;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysDictService;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.starter.common.exception.BaseException;
import com.budwk.starter.common.openapi.annotation.ApiDefinition;
import com.budwk.starter.common.openapi.annotation.ApiOperation;
import com.budwk.starter.common.openapi.annotation.ApiResponses;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.security.utils.SecurityUtil;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import lombok.extern.slf4j.Slf4j;
import org.nutz.dao.Cnd;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;

import java.util.Date;

@IocBean
@At("/platform/sqfw/sqfwNotice")
@ApiDefinition(tag = "公告")
@Slf4j
public class SqfwNoticeController {



    @Inject
    private SysUserService sysUserService;
    @Inject
    private SysDictService sysDictService;

    @Inject
    private SqfwNoticeService sqfwNoticeService;




    @At
    @Ok("json")
    @POST
    @SaCheckLogin
    public Result<?> getlistPage(@RequestBody SqfwNotice sqfwNotice) {
        Cnd cnd = Cnd.NEW();
        Sys_user  user=sysUserService.fetch(SecurityUtil.getUserId());
        if(StrUtil.isNotBlank(user.getCounty())&&!user.getCounty().equals("直属")){
            SqlExpressionGroup e1 = Cnd.exps(Sqfw_rally::getCounty,"=",user.getCounty());
            cnd.and(e1);
        }
        if(StrUtil.isNotBlank(user.getCommunity())&&!user.getCommunity().equals("直属")){
            SqlExpressionGroup e1 = Cnd.exps(Sqfw_rally::getCommunity,"=",user.getCommunity());
            cnd.and(e1);
        }
        cnd.orderBy(SqfwNotice::getCreateTime,sqfwNotice.getSort());
        Pagination getListPage=sqfwNoticeService.listPage(sqfwNotice.getPageNo(),sqfwNotice.getPageSize(),cnd);
        return Result.success(getListPage);
    }
    @At
    @Ok("json")
    @POST
    @SaCheckLogin
    public Result<?> createOne(@RequestBody SqfwNotice sqfwNotice) {
        Sys_user  user=sysUserService.fetch(SecurityUtil.getUserId());
        BeanUtil.copyProperties(user,sqfwNotice);
        sqfwNotice.setCreateTime(new Date());
        sqfwNotice.setCreateName(user.getUsername());
        sqfwNotice.setCreateBy(SecurityUtil.getUserId());
        sqfwNoticeService.insert(sqfwNotice);
        return Result.success();
    }

    @At("/updateOne")
    @Ok("json")
    @POST
    @SaCheckLogin
    public Result<?> updateOne(@RequestBody SqfwNotice sqfwNotice) {
        if(ObjectUtil.isEmpty(sqfwNotice.getNoticeId())){
            throw new BaseException("noticeId不能为空");
        }
        SqfwNotice  sqfwNoticeUpdate=   sqfwNoticeService.fetch(sqfwNotice.getNoticeId());
        if(ObjectUtil.isEmpty(sqfwNoticeUpdate)){
            throw new BaseException("noticeId错误");
        }
        BeanUtil.copyProperties(sqfwNotice,sqfwNoticeUpdate, CopyOptions.create().setIgnoreNullValue(true));
        if(!sqfwNoticeUpdate.getCreateBy().equals(SecurityUtil.getUserId())){
            throw new BaseException("不是创建人");
        }
        sqfwNoticeUpdate.setUpdateTime(new Date());
        sqfwNoticeUpdate.setUpdateBy(SecurityUtil.getUserId());
        sqfwNoticeService.update(sqfwNoticeUpdate);
        return Result.success(sqfwNoticeUpdate);
    }

    @At("/getOneInfo")
    @Ok("json")
    @POST
    @SaCheckLogin
    public Result<?> getOneInfo(@RequestBody SqfwNotice sqfwNotice) {
        if(ObjectUtil.isEmpty(sqfwNotice.getNoticeId())){
            throw new BaseException("noticeId不能为空");
        }
        SqfwNotice  sqfwNoticeInfo=   sqfwNoticeService.fetch(sqfwNotice.getNoticeId());
        return Result.success(sqfwNoticeInfo);
    }

    @At
    @Ok("json")
    @POST
    @SaCheckLogin
    public Result<?> delOne(@RequestBody SqfwNotice sqfwNotice) {
        SqfwNotice  sqfwNoticeDel=   sqfwNoticeService.fetch(sqfwNotice.getNoticeId());
        if(!sqfwNoticeDel.getCreateBy().equals(SecurityUtil.getUserId())){
            throw new BaseException("不是创建人");
        }
        sqfwNoticeService.delete(sqfwNotice.getNoticeId());
        return Result.success();
    }

}
